# -*- coding: utf-8 -*-
"""
learn.collections_dict
| @brief
| @author Zhichao Wu
| @date 2022-05-12 22:50:09
| @version 1.0.0
"""
import builtins
from collections import OrderedDict, ChainMap, Counter
from datetime import datetime
import unittest
from types import MappingProxyType


class CollectionsDictTestCase(unittest.TestCase):
    def test_OrderedDict(self):
        d1 = {'int': 123, 'str': 'abc', 'list': [1, 'a', True]}
        od = OrderedDict(d1)
        key_list1 = ['int', 'str', 'list']
        print(d1.keys())
        self.assertListEqual(key_list1, list(od.keys()))

    def test_ChainMap(self):
        pylookup = ChainMap(locals(), globals(), vars(builtins))
        print(pylookup)

    def test_Counter(self):
        dc = Counter('abcaba')
        print(dc)
        self.assertEqual(3, dc['a'])
        self.assertEqual(2, dc['b'])
        self.assertEqual(1, dc['c'])

        self.assertListEqual([('a', 3), ('b', 2)], dc.most_common(2))

    def test_MappingProxyType(self):
        d1 = {'int': 123, 'str': 'abc', 'list': [1, 'a', True]}
        dp1 = MappingProxyType(d1)

        for key in d1.keys():
            self.assertEqual(d1.get(key), dp1.get(key))

        self.assertEqual('abc', dp1.get('str'))
        d1['str'] = 'abcd'
        self.assertEqual('abcd', dp1.get('str'))


if __name__ == '__main__':
    start_time = datetime.now()
    print("[%s]%s开始运行..." % (start_time.strftime('%Y-%m-%d %H:%M:%S.%f'), __file__))

    unittest.main()

    end_time = datetime.now()
    print(f"[{end_time.strftime('%Y-%m-%d %H:%M:%S.%f')}]{__file__}结束运行.")
    print(f"耗时:{end_time - start_time}")
